package cn.superchart.module.dev.gencode.controller;

import cn.superchart.common.base.BaseController;
import cn.superchart.common.base.BaseResult;
import cn.superchart.module.dev.gencode.param.*;
import cn.superchart.module.dev.gencode.service.ScGenTableService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Validated
public class ScGenTableController extends BaseController<ScGenTableService> {



    @PostMapping("/sc/gen/table/page")
    public BaseResult<Page<ScGenTableDetailParam>> page(@RequestBody @Valid ScGenTablePageParam scGenTablePageParam) {
        Page<ScGenTableDetailParam> page = service.page(scGenTablePageParam);
        return BaseResult.result(page);
    }


    @PostMapping("/sc/gen/table/add")
    public BaseResult<String> add(@RequestBody @Valid ScGenTableAddParam scGenTableAddParam) {
        String rowId= service.add(scGenTableAddParam);
        return BaseResult.result(rowId);
    }

    @PostMapping("/sc/gen/table/edit")
    public BaseResult<String> edit(@RequestBody @Valid ScGenTableEditParam scGenTableEditParam) {
        service.edit(scGenTableEditParam);
        return BaseResult.ok();
    }

    @GetMapping("/sc/gen/table/detail")
    public BaseResult<ScGenTableDetailParam> detail( @Valid ScGenTableIdParam scGenTableIdParam) {
        return BaseResult.result(service.detail(scGenTableIdParam.getId()));
    }


    @DeleteMapping("/sc/gen/table/delete")
    public BaseResult<Boolean> delete(@Valid ScGenTableIdParam scGenTableIdParam) {
        Boolean res = service.delete(scGenTableIdParam.getId());
        return BaseResult.result(res);
    }

    @GetMapping("/sc/gen/table/dbTables")
    public BaseResult<List<ScTableDetail>> dbTables(@Valid ScTableQueryParam scTableQueryParam) {
        return BaseResult.result(service.dbTables(scTableQueryParam.getTableSchema()));
    }

    @GetMapping("/sc/gen/table/dbTableColumns")
    public BaseResult<List<ScTableColumnDetail>> dbTableColumns(@Valid ScTableQueryParam scTableQueryParam) {
        return BaseResult.result(service.dbTableColumns(scTableQueryParam.getTableSchema(),scTableQueryParam.getTableName()));
    }


    /**
     * 生成代码
     * @param scGenTableIdParam
     * @return
     */
    @GetMapping("/sc/gen/table/createCode")
    public BaseResult<String> createCode(@Valid ScGenTableIdParam scGenTableIdParam) {
        service.createCode(scGenTableIdParam.getId());
        return BaseResult.ok();
    }

}
